Lodash is a very useful utility library that lets us work with objects and arrays easily.
However, now that the JavaScript standard library is catching up to libraries such as Lodash, we can implement many of the functions in simple ways.
In this article, we look at how to clone objects in a variety of ways and do comparisons on object values.
conformsTo
The Lodash conformsTo
checks if all object entries conform to a given predicate.
We can check the value of each property to see if the condition is met as follows:
const conformsTo = (obj, predicate) => {
for (const [key, val] of Object.entries(obj)) {
if (typeof predicate[key] === 'function' && predicate[key](val)) {
return true
}
}
return false;
}
In the code above, we used the Object.entries
method to get all the entries of an object. Then we used destructuring to destructure the array entries returned by Object.entries
to the keys and values.
Then we check val
against the predicate[key]
function, which returns the true
if the condition in it is met and false
otherwise.
Then we can use it as follows:
const result = conformsTo({
a: 1
}, {
a: x => x >= 0
});
Then since a
has value 1, it conformsTo
should return true
because each value if bigger than or equal to 0 as the predicate
has condition x >= 0
as the value of the a
property.
eq
The Lodash eq
object compares 2 objects to see if they’re equivalent using the SameValueZero algorithm.
We can implement our own eq
function by doing the comparing for +0 and -0 ourselves and using Object.is
for everything else:
const eq = (a, b) => {
if (a === +0 && b === -0 ||
a === -0 && b === +0 ||
a === 0 && b === -0 ||
a === 0 && b === +0 ||
a === +0 && b === 0 ||
a === -0 && b === 0) {
return true;
}
return Object.is(a, b);
}
In the code above, we compared the 0 values with different signs and return true
for all of them.
Then we use object Object.is
for all other comparisons. When we call it as follows:
const result = eq(1, -0);
We get that result
is false
and if we call it as follows:
const result = eq(+0, -0);
we get that result
is true
.
gt
The Lodash gt
method returns a boolean indicating if one number is bigger than the other.
We can just use the > operator to do the comparison ourselves. We can implement it as follows:
const gt = (a, b) => a > b;
Then we can call it as follows:
const result = gt(2, 1);
result
is true
.
gte
Like gt
, we can implement the Lodash gte
method with the >=
operator.
We can write the following function as follows:
const gt = (a, b) => a >= b;
Then when we write the following code:
const result = gt(2, 2);
We get that result
is true
.
isArray
The Lodash isArray
method checks is a value is an array. We can just use the Array.isArray
method to check if an object is an array.
Therefore, we can write the following code to implement our own isArray
method:
const isArray = arr => Array.isArray(arr);
In the code above, we just called Array.isArray
to check if arr
is an array.
Then we can call it as follows:
const result = isArray([1, 2, 3]);
We get that result
is true
.
isDate
The Lodash isDate
method checks if an object is an instance of the Date
constructor.
We can use the constructor.name
property of an object to check what constructor the object is created from.
Therefore, we can write the following to implement our own isDate
function:
const isDate = date => date.constructor.name == 'Date'
Then we can call the function as follows:
const result = isDate(1);
and get that result
is false
. If we call it as follows:
const result = isDate(new Date());
We get that result
is true
.
Conclusion
We can use the constuctor.name
property to get the name of the constructor that an object is created from.
To check if an object is an array, we can use the Array.isArray
method to implement it.
To compare values for equality, we can use ===
and Object.is
. Finally, we can use the >
and >=
to check if something is bigger than another.